//
//  SAPCollectViewController.m
//  CarsHome
//
//  Created by dapeng on 15/12/8.
//  Copyright © 2015年 dapeng. All rights reserved.
//

#import "SAPCollectViewController.h"

@interface SAPCollectViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *titleDataSource;
@property (nonatomic, retain) NSMutableArray *priceDataSource;
@property (nonatomic, retain) NSMutableArray *pictureDataSource;
@property (nonatomic, retain) NSMutableArray *urlDataSource;
@property (nonatomic, retain) NSMutableArray *idIndex;
@property (nonatomic, retain) SAPFindCarThreeModel *threeModel;
@property (nonatomic ,retain) SAKeyValueStore *valueStore;

@end

@implementation SAPCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self tableViews];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;

    self.titleDataSource = [[NSMutableArray alloc] init];
    self.pictureDataSource = [[NSMutableArray alloc] init];
    self.priceDataSource = [[NSMutableArray alloc] init];
    self.urlDataSource = [[NSMutableArray alloc] init];
    self.idIndex = [[NSMutableArray alloc] init];
    self.valueStore = [[SAKeyValueStore alloc] initDBWithName:@"colle.db"];
    NSString *tableName = @"collect_table";
    [self.valueStore createTableWithName:tableName];
    [self.valueStore getAllItemsFromTable:tableName];
    for (SAKeyValueItem *item in [self.valueStore getAllItemsFromTable:tableName]) {
        [self.titleDataSource addObject:[item.itemObject objectForKey:@"titles"]];
        [self.pictureDataSource addObject:[item.itemObject objectForKey:@"picture"]];
        [self.priceDataSource addObject:[item.itemObject objectForKey:@"prices"]];
        [self.urlDataSource addObject:[item.itemObject objectForKey:@"ids"]];
        [self.idIndex addObject:item.itemId];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });

    
}
- (void)tableViews {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height - 60) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCellIndentifier"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCellIndentifier"];
    SAPFindCarThreeModel *threeModel = [[SAPFindCarThreeModel alloc] init];
    threeModel.imgurl = self.pictureDataSource[indexPath.row];
    cell.textLabel.text = self.titleDataSource[indexPath.row];
    cell.detailTextLabel.text = self.priceDataSource[indexPath.row];
    cell.detailTextLabel.textColor = [UIColor redColor];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:threeModel.imgurl]];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleDataSource.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    SAPSellDetailsViewController *details = [[SAPSellDetailsViewController alloc] init];
    details.url = self.urlDataSource[indexPath.row];
    [self.navigationController pushViewController:details animated:YES];
    
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"我的收藏";
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        self.valueStore = [[SAKeyValueStore alloc] initDBWithName:@"colle.db"];
        NSString *tableName = @"collect_table";
        [self.valueStore createTableWithName:tableName];
        [self.valueStore getAllItemsFromTable:tableName];
        [self.valueStore deleteObjectById:self.idIndex[indexPath.row] fromTable:tableName];
        [self.titleDataSource removeObjectAtIndex:indexPath.row];
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        [tableView reloadData];
    }
    
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
